Airbnb es una empresa

PreciosYear <-
AirbnbPrecios[, .(median_precio = median(precio, na.rm = TRUE)),
by = c("tipo","year")]
PreciosYearCoef <-
PreciosYear %>%
lm(formula = median_precio ~ year) %>%
coef()
PreciosYearColor <-
fifelse(PreciosYearCoef[2] < 0,
GreenColor, RedColor)
PreciosYearSentido <-
fifelse(PreciosYearCoef[2] < 0,
"disminuyen", "incrementan")
PreciosYearTitle <-
glue("Los precios <span style='color:{PreciosYearColor};font-weight:bold'>{PreciosYearSentido}</span>",
" en promedio ",
"<span style='color:{PreciosYearColor};font-weight:bold'>",
PreciosYearCoef[2] %>% abs() %>% dollar(accuracy = 1),
"</span> cada año",
"<br><sup>Precio por cada tipo de propiedad y año</sup>")
PreciosYearPlot <-
ggplot(PreciosYear,
aes(year, median_precio))+
geom_line(aes(group = tipo),
color = GreyColor)+
geom_point(aes(text = paste0(tipo,": ",
dollar(median_precio, accuracy = 1))),
color = GreyColor, size = 0.75)+
geom_smooth(method = "lm",
se = FALSE,
lwd = 1,
color = fifelse(PreciosYearCoef[2] > 0,
RedColor,
GreenColor) )+
geom_text(aes(label = tipo,
x = year +0.25),
color = GreyColor,
size = 3.5,
data = PreciosYear[year == max(year)])+
scale_y_log10(breaks = breaks_log(7),
labels = dollar_format(accuracy = 1))+
expand_limits(x = max(PreciosYear$year)+1)
ggplotly(PreciosYearPlot, tooltip = "text") %>%
layout(title = list(text = PreciosYearTitle,
xref="paper",
x=0,
font = list(size = 16)),
xaxis = list(title = list(text = "Año",
font = list(size = 14))),
yaxis = list(title = list(text = "Precio de las propiedades",
font = list(size = 14))))
PreciosProvincia <-
AirbnbPrecios[, .(median_precio = median(precio, na.rm = TRUE),
tipos_no_disponibles =
unique(AirbnbPrecios$tipo) %>%
sort() %>%
setdiff(tipo[precio>0]) %>%
paste0(collapse = ", ")),
by = "provincia"
][median_precio > 0
][,`:=`(provincia = fct_reorder(provincia, median_precio, sum, .desc = TRUE),
higher_value = median_precio == max(median_precio))]
PreciosProvinciaPlot <-
ggplot(PreciosProvincia,
aes(median_precio, fct_rev(provincia)))+
geom_col(aes(fill = higher_value,
text =
fifelse(tipos_no_disponibles %like% "\\w",
paste0("<br>Propiedades ausentes: ",
tipos_no_disponibles),
"") %>%
paste0("Mediana: ", dollar(median_precio, accuracy = 1),
next_line = .)))+
scale_fill_manual(values = c("TRUE" = RedColor,
"FALSE" = GreyColor))+
scale_x_continuous(breaks = breaks_width(200),
labels = dollar_format(accuracy = 1))+
theme(legend.position = "none")
PreciosProvinciaTitle <-
glue("<span style='color:{RedColor};font-weight:bold'>",
PreciosProvincia[higher_value == TRUE,
as.character(provincia)],
"</span> tiene los precios más elevados",
"<br><sup>Precio de las propiedades por provincia</sup>")
ggplotly(PreciosProvinciaPlot, tooltip = "text") %>%
layout(title = list(text = PreciosProvinciaTitle,
xref="paper",
x=0,
font = list(size = 16)),
xaxis = list(title = list(text = "Precio de las propiedades",
font = list(size = 14))),
yaxis = list(title = list(text = "Provincias",
font = list(size = 14))))
PreciosMonth <-
AirbnbPrecios[tipo == fread(here("model-output/propiedades-monthly-efect.csv"))[[1]][6] &
provincia == "Valverde",
.(mediana_precio = median(precio)),
by = "month"
][,`:=`(max_month = mediana_precio == max(mediana_precio) |
mediana_precio / max(mediana_precio) >= 0.95,
min_month = mediana_precio == min(mediana_precio) |
mediana_precio / min(mediana_precio) <= 1.05)
][,{ var_to_report <-
if(sum(max_month) < sum(min_month)){"max_month"}else{"min_month"}
copy(.SD)[, `:=`(selected_var = var_to_report,
result_month = .SD[[var_to_report]])] }]
MonthColor <-
if(unique(PreciosMonth$selected_var) == "max_month") {
RedColor
}else{
GreenColor
}
PreciosMonthTitle <-
PreciosMonth[result_month == TRUE,
month.name[as.integer(month)]] %>%
paste0(glue("<span style='color:{MonthColor};font-weight:bold'>"),
month = ., "</span>") %>%
(\(x) if(length(x) == 1){
paste0(x, " es el mes ")
}else{
paste0(x[-length(x)], collapse = ", ") %>%
paste0(" y ", tail(x,1)," son los meses ")
})() %>%
paste0(fifelse(MonthColor == RedColor,
"con mayor precio",
"con menor precio"),
"<br><sup>Precio de las propiedades por mes</sup>")
PreciosMonthPlot <-
PreciosMonth %>%
ggplot(aes(mediana_precio,
fct_rev(month),
color = result_month))+
geom_point(aes(text = dollar(mediana_precio, accuracy = 1)),
size = 4)+
geom_segment(aes(yend = month, xend = mediana_precio),
x = 0,
linewidth = 1.5)+
scale_x_continuous(labels = dollar_format(accuracy = 1))+
scale_color_manual(values = c("FALSE" = GreyColor,
"TRUE" = MonthColor))+
theme(legend.position = "none")
ggplotly(PreciosMonthPlot, tooltip = "text") %>%
layout(title = list(text = PreciosMonthTitle,
xref="paper",
x=0,
font = list(size = 16)),
xaxis = list(title = list(text = "Precio de las propiedades",
font = list(size = 14))),
yaxis = list(title = list(text = "Meses",
font = list(size = 14))))
HabitacionesActivasTitle <-
HabitacionesActivas[, .(total_level = sum(color_level == "4")),
by = "provincia"
][total_level == max(total_level), provincia] %>%
paste0("<b>", provincia = ., "</b>") %>%
(\(x) if(length(x) == 1){
paste0(x, " es la provincia ")
}else{
paste0(x[-length(x)], collapse = ", ") %>%
paste0(" y ", tail(x,1)," son las provincias ")
})() %>%
paste0("con la mayor disponibilidad de habitaciones",
"<br><sup>Habitaciones disponibles por provincia</sup>")
HabitacionesActivasPlot <-
ggplot(HabitacionesActivas,
aes(n_habitaciones, provincia))+
geom_tile(aes(fill = color_level),
color = "white")+
geom_text(aes(label = comma(propiedades_activas, accuracy = 1),
color = color_level))+
scale_fill_manual(values = c("white","#c6c6c6","#c88983","#bc4646") )+
scale_color_manual(values = c(GreyColor,"grey50", "white", "white") )+
theme(legend.position = "none",
axis.line = element_blank())
ggplotly(HabitacionesActivasPlot, tooltip = "text") %>%
layout(title = list(text = HabitacionesActivasTitle,
xref="paper",
x=0,
font = list(size = 16)),
xaxis = list(title = list(text = "Número de habitaciones",
font = list(size = 14))),
yaxis = list(title = list(text = "Provincia",
font = list(size = 14))))